import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';

import { Meteor } from 'meteor/meteor';

class AnFangsCollection extends Mongo.Collection {
    update(selector, modifier) {
        const result = super.update(selector, modifier);
        return result;
    }
    insert(log, callback) {
        const ourAnFangs = log;
        const result = super.insert(ourAnFangs, callback);
        return result;
    }
    remove(selector, callback) {
        const result = super.remove(selector, callback);
        return result;
    }
}

export const AnFangs = new AnFangsCollection('an_fangs');

AnFangs.deny({
    insert() { return true; },
    update() { return true; },
    remove() { return true; },
});

AnFangs.schema = new SimpleSchema({

    name:{
        label:"安防名称",
        type:String
    },
    familyId:{
        label:"所属家庭ID",
        type:String
    },
    status:{
        label:"状态",
        type: Boolean
    },
    devices:{
        label: "所包含的设备",
        type: [Object],
        optional: true
    },
    createdName: {
        label: '创建者',
        type: String,
        optional: true
    },
    'devices.$._id':{
        label: "设备ID",
        type: String,
        optional: true
    },
    'devices.$.gatewayId':{
        label: "设备所属网关ID",
        type: String,
        optional: true
    },
    'devices.$.state':{
        label:"设备状态",
        type: String,
        optional: true
    },
    'devices.$.d_name': {
        type: String,
        label: '设备名称',
        optional: true
    },
    updatedAt: {
        type: Date,
        optional: true,
        autoValue() {
            if (this.isUpdate) {
                return new Date();
            }
        }
    },
    createdAt: {
        type: Date,
        optional: true,
        label: '创建时间',
        autoValue() {
            if (this.isInsert) {
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }
            return this.unset();  // Prevent user from supplying their own value
        }
    }

});
AnFangs.attachSchema(AnFangs.schema);
